package java.wxw;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.Scanner;

/**
 * @author weisuhang
 */
public class WriteFile extends Final {

    public static void write(){
       String str,str1;
       int i,k,n=0;
       String filename = "";
       String text = "";
       if (dirName.replaceAll(" ","").equals("")){
           System.out.println("Error. 请先转入用户目录下");
       }else {
           System.out.println("Caution: Open file first");
           System.out.println("Open File(s) List: ");
           k = new DirExist().dirExist(dirName); // 第几个用户
           for (i = 0; i < fileCount[k]; i++){ // 第k个用户有多少文件
               if (openIf[k][i].openIf == 1){ //如果文件已经被打开
                   if (openIf[k][i].openMode == 0 || openIf[k][i].openMode == 2){ //
                       System.out.println(userDirs[k].userFile[i].fileName);
                       n++;
                   }
                   if(n%4==0&&n!=0)
                       System.out.println(); // 每四个换行
               }
           }
           System.out.println(n+" files openned");
           if (n == 0) return; // 无打开的文件返回
           if (n != 0) { // 有打开的文件
               System.out.println("Please input FileName");
               Scanner scanner = new Scanner(System.in);
               if (scanner.hasNext()) filename = scanner.next();
               filename.replaceAll(" ","");
               i = new FileExist().fileExsit(filename); // 第几个文件号
               if (i >= 0){ // 文件存在
                   if (openIf[k][i].openIf==1){  // 已打开
                       if (openIf[k][i].openMode==1||openIf[k][i].openMode==2){ // 可读
                           str = String.valueOf(userDirs[k].userFile[i].filePANo); // 文件号转为字符串
                           str1 = "file" + str;
                           str = "c:\\\\osfile\\\\file\\\\";
                           str = str + str1;
                           try {
                               FileInputStream fileInputStream = new FileInputStream(str); // 读取文件
                               System.out.println("The text is: \n");
                               byte[] buff = new byte[1024];
                               int len = 0;
                               while ((len=fileInputStream.read(buff))!=-1){
                                   text = new String(buff,0,len);
                               }
                               System.out.println(text);
                               fileInputStream.close();
                           } catch (FileNotFoundException e) {
                               e.printStackTrace();
                           } catch (IOException e) {
                               e.printStackTrace();
                           }
                       }else { // 不可读
                           System.out.println("Error! " + filename + "has been opened with WRITE ONLY mode. It isn't");
                       }
                   }else{ // 未打开
                       System.out.println("Error! " + filename + "is in closing status. Please open it before read.");
                   }
               }else { // 文件不存在
                   System.out.println("Error! " + filename + "does not exit.");
               }
           }
       }

    }
}
